package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.XcxMenu;
import com.ruoyi.system.service.IXcxMenuService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 小程序菜单Controller
 *
 * @author ruoyi
 * @date 2022-12-29
 */
@Api(tags = "小程序菜单模块接口")
@RestController
@RequestMapping("/xcxmenu")
public class XcxMenuController extends BaseController
{
    @Autowired
    private IXcxMenuService xcxMenuService;


//    @ApiOperation(notes = "", value = "获取小程序权限")
//    @PostMapping("/GetxcxRuot")
//    public List<Long> GetxcxRuot(@RequestBody Long id){
//        return xcxMenuService.GetxcxRuot(id);
//    }


    /**
     * 加载所有小程序角色菜单列表树
     */
    @ApiOperation(notes = "", value = "测试树形结构目录树")
    @PostMapping("/roleXcxMenuTreeselect")
    public AjaxResult roleXcxMenuTreeselect(){
        Long userId = SecurityUtils.getUserId();
//        System.out.println("userid"+userId);
        List<XcxMenu> xcxMenus=xcxMenuService.selectXcxMenuList(userId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("xcxmenus",xcxMenuService.buildXcxMenuTreeSelect(xcxMenus));
        return ajax;
    }



    /**
     * 加载所有小程序角色菜单列表树
     */
    @ApiOperation(notes = "", value = "根据角色ID查询小程序菜单权限")
    @PostMapping("/selectXcxMenuListS")
    public AjaxResult selectXcxMenuListS(String roleid){
        List<XcxMenu> xcxMenus=xcxMenuService.selectXcxMenuListS(roleid);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("xcxmenus",xcxMenus);
        return ajax;
    }


//    ============================================================ >>>>>>           等待测试接口

    /**
     * 查询小程序菜单列表
     */
    @RequiresPermissions("system:xcxmenu:list")
    @GetMapping("/list")
    public TableDataInfo list(XcxMenu xcxMenu)
    {
        startPage();
        List<XcxMenu> list = xcxMenuService.selectXcxMenuList(xcxMenu);
        return getDataTable(list);
    }

    /**
     * 导出小程序菜单列表
     */
    @RequiresPermissions("system:xcxmenu:export")
    @Log(title = "小程序菜单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XcxMenu xcxMenu)
    {
        List<XcxMenu> list = xcxMenuService.selectXcxMenuList(xcxMenu);
        ExcelUtil<XcxMenu> util = new ExcelUtil<XcxMenu>(XcxMenu.class);
        util.exportExcel(response, list, "小程序菜单数据");
    }

    /**
     * 获取小程序菜单详细信息
     */
    @RequiresPermissions("system:xcxmenu:query")
    @GetMapping(value = "/{xcxMenuId}")
    public AjaxResult getInfo(@PathVariable("xcxMenuId") Long xcxMenuId)
    {
        return success(xcxMenuService.selectXcxMenuByXcxMenuId(xcxMenuId));
    }

    /**
     * 新增小程序菜单
     */
    @RequiresPermissions("system:xcxmenu:add")
    @Log(title = "小程序菜单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XcxMenu xcxMenu)
    {
        return toAjax(xcxMenuService.insertXcxMenu(xcxMenu));
    }

    /**
     * 修改小程序菜单
     */
    @RequiresPermissions("system:xcxmenu:edit")
    @Log(title = "小程序菜单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XcxMenu xcxMenu)
    {
        return toAjax(xcxMenuService.updateXcxMenu(xcxMenu));
    }

    /**
     * 删除小程序菜单
     */
    @RequiresPermissions("system:xcxmenu:remove")
    @Log(title = "小程序菜单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{xcxMenuIds}")
    public AjaxResult remove(@PathVariable Long[] xcxMenuIds)
    {
        return toAjax(xcxMenuService.deleteXcxMenuByXcxMenuIds(xcxMenuIds));
    }
}
